package com.zhangmeng.tools.controller;

import cn.hutool.core.text.StrFormatter;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ImagePath;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;
import java.nio.charset.StandardCharsets;


/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-28 17:07
 */
@Slf4j
public class MavenInstallJarController {

    @FXML
    public TextField jar_path;

    @FXML
    public TextField group_id;

    @FXML
    public TextField artifact_id;

    @FXML
    public TextField version;

    @FXML
    public Button install;

    @FXML
    public TextArea textArea;

    @FXML
    public Button choose_file;

    public static final String cmd = "cmd /c mvn install:install-file -Dfile={} -DgroupId={} -DartifactId={} -Dversion={} -Dpackaging=jar";

    @FXML
    public void initialize() {

        choose_file.setText(null);
        ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
        iv.setPreserveRatio(true);
        iv.setFitWidth(18);
        choose_file.setGraphic(iv);

        choose_file.setOnAction(event -> {
            choose_file();
        });

        install.setOnAction(event -> {
            if (jar_path.getText().length() == 0){
                AlertUtils.alert_warning("安装jar包路径不能为空!");
                return;
            }

            if (group_id.getText().length() == 0){
                AlertUtils.alert_warning("group_id不能为空!");
                return;
            }

            if (artifact_id.getText().length() == 0){
                AlertUtils.alert_warning("artifact_id不能为空!");
                return;
            }

            if (version.getText().length() == 0){
                AlertUtils.alert_warning("version不能为空!");
                return;
            }
            String format = StrFormatter.format(cmd, jar_path.getText(), group_id.getText(), artifact_id.getText(), version.getText());
            log.info(format);
            exec(format);
        });
    }

    public void choose_file() {
        Stage stage = new Stage();
        FileChooser fc = new FileChooser();
        fc.setTitle("单选文件");
        fc.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("jar文件", "*.jar")
        );
        File file = fc.showOpenDialog(stage);
        if (file == null) {
            return;
        }
        jar_path.setText(file.getPath());
    }

    public void exec(String cmd) {
        textArea.setText(null);
        // Java调用 dos命令
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"GBK");
            BufferedReader br = new BufferedReader(isr);
            String content = br.readLine();
            while (content != null) {
                add_msg(content);
                content = br.readLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void add_msg(String content){
        textArea.appendText(System.lineSeparator());
        textArea.appendText(content);
        textArea.appendText(System.lineSeparator());
    }
}
